<?php

class MenuNode {
	public $title;
	public $uri;
	private $children;
	public function __construct( $title, $uri ) {
		$this->title = $title;
		$this->uri = $uri;
	}
	public function getChildren() {
		return $this->children;
	}
	
	public function setChildren( $children ) {
		$this->children = $children;
	}
}

class MenuBean {
	
	private $tree;
	
	public function getTree() {
		if( $this->tree == null ) {
			$this->tree = new MenuNode( "PhpFaces Site", "home.html" );
			$children = array( 
				new MenuNode( "Home", "home.html" ),
				new MenuNode( "Status", "status.html" ),
				new MenuNode( "About", "about.html" ),
				new MenuNode( "Download", "download.html" ),
				new MenuNode( "Documentation", "doc" ),
				new MenuNode( "FAQ", "php-programmers-faq.html" ),
				new MenuNode( "Show Case", "showcase.html" )
			);
			
			$this->tree->setChildren( $children );
		}
		
		return $this->tree;
	}
	
	public function getCurrentNode() {
		return $this->findNodeByUri( "home.html" );
	}
	
	public function findNodeByUri( $uri, $root = null ) {
		if( $root == null ) {
			$root = $this->tree;
		}

		foreach( $root->getChildren() as $child ) {
			if( $child->uri == $uri ) {
				return $child;
			} else {
				$found = $this->findNodeByUri( $uri, $child );
				if( $found != null ) {
					return $found;
				}
			}
		}
		
		return null;

	}
}
 
?>
